package com.cfjjc.gz.common.util;


import java.security.*;

public class MD5 {

	// 十六进制下数字到字符的映射数组
	private final static String[] hexDigits =

	{ "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };

	/**
	 * 验证输入的密码是否正确
	 * 
	 * @param password 真正的密码（加密后的真密码）
	 * @param inputString 输入的字符串
	 * @return 验证结果，boolean类型
	 */
	public static boolean compare(String md5Str, String inputString) {
		if (md5Str.equals(encodeByMD5(inputString))) {
			return true;
		} else {
			return false;
		}
	}

	/** 对字符串进行MD5编码 */
	public static String encodeByMD5(String inputString) {
		if (inputString == null || "".equals(inputString)) {
			throw new IllegalArgumentException("");
		}
		try {
			// 创建具有指定算法名称的信息摘要
			MessageDigest md = MessageDigest.getInstance("MD5");
			// 使用指定的字节数组对摘要进行最后更新，然后完成摘要计算
			byte[] results = md.digest(inputString.getBytes());
			// 将得到的字节数组变成字符串返回
			String resultString = byteArrayToHexString(results);
			return resultString.toUpperCase();
		} catch (Exception ex) {
			ex.printStackTrace();
			return null;
		}
	}

	/**
	 * 轮换字节数组为十六进制字符串
	 * 
	 * @param b 字节数组
	 * @return 十六进制字符串
	 */
	private static String byteArrayToHexString(byte[] b) {
		StringBuffer resultSb = new StringBuffer();
		for (int i = 0; i < b.length; i++) {
			resultSb.append(byteToHexString(b[i]));
		}
		return resultSb.toString();
	}

	/**
	 * 将一个字节转化成十六进制形式的字符串
	 */
	private static String byteToHexString(byte b) {
		int n = b;
		if (n < 0)
			n = 256 + n;
		int d1 = n / 16;
		int d2 = n % 16;
		return hexDigits[d1] + hexDigits[d2];
	}

	public static void main(String[] args) {
		String password = MD5.encodeByMD5("111111");
		System.out.println("对admin用MD5摘要后的字符串：" + password);
		String inputString = "111111";
		System.out.println("tianshi0253与密码匹配?" + MD5.compare(password, inputString));
		inputString = "111111";
		System.out.println("admin与密码匹配?" + MD5.compare(password, inputString));
		
		String startIp = "192.168.15.101";
		String endIp = "191.168.25.115";
		
		String a = startIp.replace(".", "");
		
		System.out.println(a);
		
		Long long_startIp = new Long(String.valueOf(startIp.replace(".", "")));
		Long long_endIp = new Long(String.valueOf(endIp.replace(".", "")));
		
		String tmp = "192.168.21.100";
		Long long_tmp = new Long(String.valueOf(tmp.replace(".", "")));
		
		if(long_tmp>=long_startIp && long_tmp<=long_endIp){
			System.out.println("允许访问！");
		}else{
			System.out.println("非法IP地址！");
		}
		
		
		String[] ips = startIp.split("\\.");
		long n = 0;
		for (int i=0; i<ips.length; i++) {
		  n = n<<8 + Integer.parseInt(ips[i]);
		  System.out.println(n);
		}
		
		String startips[] = startIp.split("\\.");
		String endIps[] = endIp.split("\\.");
		for (int i = 0; i < startips.length; i++) {
			if (Integer.parseInt(endIps[i]) > Integer.parseInt(startips[i])) {
				System.out.println("通过");
				break;
			} else {
				if (Integer.parseInt(endIps[i]) == Integer.parseInt(startips[i])) {
					System.out.println(endIps[i] + "相等");
					continue;
				} else {
					System.out.println("结束IP必须大于开始IP！");
					break;
				}
			}
		}
		System.out.println(endIps);
		
	}

}
